var form ;
$(function(){
    layui.use(['layer', 'form'], function() {
        form = layui.form();
        init();
    })

});
function init(){
    getSupplyInfo();
    getArea(1,1);
    $('.btn').bind('click',function(){
        submitConsMsg();
    });
}
var getSupplyInfo = function(){
    var userId = userid;

    $.ajax({
        url:server_url+"rest/supply/register/selectByUserId.do?userId="+userId,
        type:'post',
        ansyc:false,
        data:{ran:Math.random()},
        error:function(){easyAlert(contants.systemError);},
        success:function(msg){
            console.log(msg);
            if(msg.data.province != ''){
                $("#consProvince").val(msg.data.province)
                getArea(msg.data.province, 2);
                $("#consCity1").val(msg.data.city)
                $("#consCity").val(msg.data.city)
                getArea(msg.data.city, 3);
                $("#consCounty1").val(msg.data.county)
                $("#consCounty").val(msg.data.county)
                $("#addr").val(msg.data.address)
                form.render("select");
            }
            $("#supplyId").val(msg.data.supplyid);
        }
    });
}

var getArea = function(parent,servel){
    $.ajax({
        url:server_url+'rest/areaManager/getByParent.do?parentId='+parent,
        type:'get',
        async:false,
        data:{ran:Math.random()},
        error:function(){easyAlert(contants.systemError);},
        success:function(msg){
            //console.log(msg);
            var areas = msg.data;
            var s = '';
            for(var i=0;i<areas.length;i++){
                s +='<option value="'+areas[i].areaId+'">'+areas[i].areaName+'</option>';
            }
            if(servel ==1){
                s= '<option value="">请选择省</option>' +s;
                $("#consProvince").html(s);
                form.render("select");
                form.on('select(consProvince)', function(data){
                    var pro = data.value;
                    $("#consProvince").val(pro);
                    getArea(pro,2);
                });
            }else if(servel == 2){
                s= '<option value="">请选择市</option>' +s;
                $("#consCity1").html(s);
                form.render("select");
                form.on('select(consCity)', function(data){
                    $("#consCity").val(data.value);
                    console.log($('#consCity').val())
                    getArea(data.value,3)
                });
            }else if(servel == 3){
                s= '<option value="">请选择县</option>' +s;
                $("#consCounty1").html(s);
                form.render("select");
                form.on('select(consCounty)', function(data){
                    console.log(data.value)
                    var pro = data.value;
                    $("#consCounty").val(pro);
                });
            }

        }

    })
}
var submitConsMsg = function(){
    var jsonParam=[];
    if($('#consProvince').val() != null &&$('#consProvince').val() != ''){
        jsonParam.push({name:'province',value:$('#consProvince').val()});
    }else{
        easyAlert('请选择省');return;
    }
    if($('#consCity').val() != null && $('#consCity').val() != ''){
        jsonParam.push({name:'city',value:$('#consCity').val()});
    }else{
        easyAlert('请选择市');return;
    }
    if($('#consCounty').val() != null &&$('#consCounty').val() != ''){
        jsonParam.push({name:'county',value:$('#consCounty').val()});
    }else{
        easyAlert('请选择县');return;
    }
    if($('#addr').val() != null && $('#addr').val()!= ''){
        jsonParam.push({name:'address',value:$('#addr').val()});
    }else{
        easyAlert("请填写详细地址");return;
    }
    jsonParam.push({name:"supplyid",value:$("#supplyId").val()});
    console.log(jsonParam);
    $.ajax({
        url:server_url+'/rest/supply/personal/updateAddress.do?ran='+Math.random(),
        type:'post',
        data:jsonParam,
        error:function(){easyAlert(contants.systemError);},
        success:function(msg){
            goPerson();
        }
    });
}
var goPerson = function(){
    window.location.href='/LianniuWeb/supply/personal/informationDetail.do';
}